home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 001-025 / disk_010 / iff / remalloc.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  2KB  |  71 lines

  1. /*-- RemAlloc.c --------------------------------------------------------*/
  2. /*  ChipAlloc(), ExtAlloc(), RemAlloc(), RemFree().                     */
  3. /*  ALLOCators which REMember the size allocated, for simpler freeing.  */
  4. /*----------------------------------------------------------------------*/
  5.  
  6. #include <exec/types.h>
  7. #include <exec/nodes.h>
  8. #include <exec/memory.h>
  9.  
  10.  
  11. /*--------------- Function declarations for MANX ---------------*/
  12.  
  13. extern void *AllocMem();
  14.  
  15. /* first 4 bytes of allocated area contain the length of the area.
  16.  * This length is the number of bytes allocated by AllocMem, ie includes
  17.  * the 4 bytes for itself */
  18.  
  19. /* size in bytes.  That is what the "sizeof" function returns.
  20.  * Example:
  21.  *   struct BitMap *bm;
  22.  *   bm = RemAlloc( sizeof(struct BitMap), ...flags... );
  23.  */
  24. UBYTE *RemAlloc(size,flags)
  25.  LONG size, flags;
  26.  {
  27.     register LONG *p;
  28.     register LONG asize = size+4;
  29.     p = (LONG *)AllocMem(asize,flags);
  30.     if (p != NULL)
  31.         *p++ = asize;   /* post-bump p to point at clients area*/
  32.     return((UBYTE *)p);
  33.  }
  34.  
  35. /* ALLOCator that remembers size, allocates in CHIP-accessable memory.
  36.  * Use for all data to be displayed on screen, all sound data, all data to be
  37.  * blitted, disk buffers, or access by any other DMA channel.
  38.  * Does clear memory being allocated.*/
  39.  
  40. UBYTE *ChipAlloc(size)
  41. LONG size;
  42. {
  43.     return(RemAlloc(size, MEMF_CLEAR|MEMF_PUBLIC|MEMF_CHIP));
  44. }
  45.  
  46. /* ALLOCator that remembers size, allocates in extended memory.
  47.  * Does clear memory being allocated.
  48.  * NOTICE: does NOT declare "MEMF_FAST".  This allows machines
  49.  * lacking extended memory to allocate within chip memory,
  50.  * assuming there is enough memory left.  */
  51.  
  52. UBYTE *ExtAlloc(size)
  53. LONG size;
  54. {
  55.     return(RemAlloc(size, MEMF_CLEAR|MEMF_PUBLIC));
  56. }
  57.  
  58. /* FREEs either chip or extended memory, if allocated with an allocator
  59.  * which REMembers size allocated.
  60.  * Safe: won't attempt to de-allocate a NULL pointer.*/
  61.  
  62. RemFree(p)
  63. UBYTE *p;
  64. {
  65.     if (p != NULL) {
  66.         p -= 4;
  67.         return(FreeMem(p, *((LONG *)p)));
  68.     }
  69. }
  70.  
  71.